package com.wyd.observer.demo01;

/**
 * @author Yudi Wang
 * @date 2022/1/6 10:58
 */
public class WeatherData {
    private TemperatureDisplay temperatureDisplay;
    private PressureDisplay pressureDisplay;

    public void setTemperatureDisplay(TemperatureDisplay temperatureDisplay) {
        this.temperatureDisplay = temperatureDisplay;
    }

    public void setPressureDisplay(PressureDisplay pressureDisplay) {
        this.pressureDisplay = pressureDisplay;
    }

    /**
     * 气温
     */
    private int temperature;
    /**
     * 气压
     */
    private int pressure;

    /**
     * 模拟监测值发生变化
     * @param temperature
     * @param pressure
     */
    public void setMessureData(int temperature, int pressure) {
        this.temperature = temperature;
        this.pressure = pressure;
        messureDataChanged();
    }

    /**
     * 当监测值发生变化时，将会调用该方法
     *
     * 代码结构存在的问题如下：
     * temperatureDisplay 为具体实现类，没有遵循面向接口编程原则
     * 无法动态增加 Display 显示器对象，而必须修改 WeatherData 的源代码
     * update 的方法参数不便于扩展
     */
    public void messureDataChanged(){
        temperatureDisplay.update(temperature, pressure);
        pressureDisplay.update(temperature, pressure);
    }


    public static void main(String[] args) {
        WeatherData weatherData = new WeatherData();
        weatherData.setPressureDisplay(new PressureDisplay());
        weatherData.setTemperatureDisplay(new TemperatureDisplay());
        //模拟监测值变化
        weatherData.setMessureData(33, 101);
    }
}
